package habitcoach.controller;

import habitcoach.core.habit.Habit;
import habitcoach.core.habit.HabitFactory;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import util.Constants;

/**
 * Manager to manage habits
 * 
 * @author Coddy Created at 2010-2-7 上午11:49:27
 */
public class DefaultHabitManager implements HabitManager
{
	private volatile static HabitManager instance;
	List<Habit> habitList;
	private static Log log = LogFactory.getLog(DefaultHabitManager.class);

	private DefaultHabitManager()
	{
		super();
		habitList = new ArrayList<Habit>();
	}

	/**
	 * Entry to get HabitManager
	 * 
	 * @return
	 */
	public static HabitManager getInstance()
	{
		if (instance == null)
		{
			synchronized (DefaultHabitManager.class)
			{
				if (instance == null)
				{
					instance = new DefaultHabitManager();
					log.debug("Created " + DefaultHabitManager.class);
				}
			}
		}
		return instance;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * habitcoach.core.habit.IHabitManager#addHabit(habitcoach.core.habit.Habit)
	 */
	public void addHabit(Habit habit)
	{
		habitList.add(habit);
		log.debug("Add Habit.");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * habitcoach.core.habit.IHabitManager#removeHabit(habitcoach.core.habit
	 * .Habit)
	 */
	public void removeHabit(Habit habit)
	{
		habitList.remove(habit);
		log.debug("Remove Habit.");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see habitcoach.controller.HabitManager#addHabit(java.lang.String)
	 */
	@Override
	public void addHabit(String question)
	{
		Habit habit = HabitFactory.getInstance().createHabit(
				Constants.DEFAULT_TITLE, Constants.DEFAULT_CATEGORY, question);
		addHabit(habit);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see habitcoach.controller.HabitManager#getHabitList()
	 */
	@Override
	public List<Habit> getHabitList()
	{
		return habitList;
	}

}
